import { defineStore } from 'pinia';
import { storage } from '@/utils/LocalStorage.ts';
import { ref } from 'vue';
import operateAccount from '@/apis/operate-account.ts';
import { useTeamStore } from '@/store/useTeamStore.ts';

export const useAccountStore = defineStore('account', () => {
  // state 属性
  const userToken = ref(storage.get('satoken') || '');
  const nickname = ref('');
  const avatarUrl = ref('');
  const account = ref('');
  // 保存token
  function setUserToken(token: string) {
    userToken.value = token;
    storage.set('satoken', token);
  }
  // 删除token
  function removeToken() {
    userToken.value = '';
    storage.removeAll();
  }
  // 保存用户昵称
  function setNickname(nickName: string) {
    nickname.value = nickName;
  }
  // 保存头像URL
  function setAvatarUrl(url: string) {
    avatarUrl.value = url;
  }
  // 保存账号
  function setAccount(acc: string) {
    account.value = acc;
  }

  // 上传图片 请求操作
  async function uploadImg(params: any) {
    try {
      const res = await operateAccount.uploadImg(params);
      return res?.data;
    } catch (e) {
      console.error(e);
    }
  }
  // 修改个人信息 请求操作
  async function updateAccountInfo(nickName?: string, avatarUrl?: string) {
    try {
      if (!nickName && !avatarUrl) return;
      let params: any = {};
      if (nickName) {
        params.newNickname = nickName;
      }
      if (avatarUrl) {
        params.newAvatarUrl = avatarUrl;
      }
      return await operateAccount.updateAccountInfo(params);
    } catch (e) {
      console.error(e);
    }
  }
  // 重置密码 请求操作
  async function resetPassword(newPw: string, checkPw: string) {
    try {
      if (!newPw && !checkPw) return;
      return await operateAccount.resetPassword({
        newPassword: newPw,
        newPasswordConfirm: checkPw,
      });
    } catch (e) {
      console.error(e);
    }
  }
  // 退出登录 请求操作
  async function logout() {
    try {
      await operateAccount.logout();
      const teamStore = useTeamStore();
      teamStore.removeCurrentTeamId();
      removeToken();
    } catch (e) {
      console.error(e);
    }
  }
  // 检查换绑手机号 请求操作
  async function checkNewPhone(newPhone: string) {
    try {
      if (!newPhone) return;
      return await operateAccount.checkNewPhone({
        phoneNum: newPhone,
      });
    } catch (e) {
      console.error(e);
    }
  }
  // 换绑手机号 请求操作
  async function updatePhone(newPhone: string, verifyCode: string) {
    try {
      if (!newPhone || !verifyCode) return;
      return await operateAccount.updatePhone({
        phoneNum: newPhone,
        verifyCode,
      });
    } catch (e) {
      console.error(e);
    }
  }
  // 获取账号信息
  async function getAccountInfo() {
    try {
      const res = await operateAccount.getAccountInfo();
      setNickname(res?.data.nickname);
      setAvatarUrl(res?.data.avatarUrl);
      setAccount(res?.data.phoneNum);
    } catch (e) {
      console.error(e);
    }
  }

  return {
    userToken,
    setUserToken,
    removeToken,
    nickname,
    setNickname,
    avatarUrl,
    setAvatarUrl,
    account,
    setAccount,

    uploadImg,
    updateAccountInfo,
    resetPassword,
    logout,
    checkNewPhone,
    updatePhone,
    getAccountInfo,
  };
});
